// pages/deliver-goods/deliver-goods-detail.js
//获取应用实例
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
import Dialog from 'vant-weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    deliverGoodsData: {},
    selectDriverShow: false,
    driverList: [],
    selectDriverId: "",
    orderId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.orderId = options.orderId;
    http.purchaseorderDetail(this.data.orderId).then(res => {
      if (res.code == 0) {
        res.data.endTime = res.data.arrivalDateEnd.split(" ")[1];
        res.data.productslen = Object.getOwnPropertyNames(res.data.products).length;
        this.setData({
          deliverGoodsData: res.data
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  selectDriver() {
    let personInfo = userInfoManager.getSupplierInfo();
    http.driverList({
      supplierId: personInfo.id
    }).then(res => {
      if (res.code == 0) {
        let driverList = [];
        let drivers = [];
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].isSelect = false;
          if ((i + 1) % 6 === 0) {
            drivers.push(res.data[i]);
            driverList.push(drivers);
            drivers = [];
          } else {
            drivers.push(res.data[i]);
            if (i === res.data.length-1){
              driverList.push(drivers);
            }
          }
        }
        this.setData({
          selectDriverShow: true,
          driverList: driverList
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })

  },
  /**关闭司机组件 */
  onSelectDriverClose() {
    this.setData({
      selectDriverShow: false
    })
  },
  clickAddress(e) {
    let order = e.currentTarget.dataset.order;
    Dialog.alert({
      title: order.schoolName,
      message: order.schoolAddr,
      confirmButtonColor: "#21af65",
    }).then(() => {
      // on close
    });
  },
  clickPhone(e) {
    let order = e.currentTarget.dataset.order;
    Dialog.confirm({
      title: order.schoolName,
      message: order.schoolTel + '\n负责人：' + order.adminName,
      showCancelButton: true,
      confirmButtonText: "拨打电话",
      confirmButtonColor: "#21af65",
      cancelButtonColor: "#21af65"
    }).then(() => {
      if (order.schoolTel && order.schoolTel !== "") {
        wx.makePhoneCall({
          phoneNumber: order.schoolTel //仅为示例，并非真实的电话号码
        })
      }
    }).catch(() => {
      // on cancel
    });
  },
  /**选择司机  */
  clickDriver(event) {
    if (event.currentTarget.dataset.item.isSelect) return;
    for (let i = 0; i < this.data.driverList.length; i++) {
      for (let n = 0; n < this.data.driverList[i].length; n++) {
        if (this.data.driverList[i][n].id === event.currentTarget.dataset.item.id) {
          this.data.driverList[i][n].isSelect = true;
          this.data.selectDriverId = this.data.driverList[i][n].id;
        } else {
          this.data.driverList[i][n].isSelect = false;
        }
      }
    }
    this.setData({
      driverList: this.data.driverList
    });
  },
  /**确定发货 */
  clickSure(e){
    console.log("clickSure");
    if (this.data.selectDriverId === ""){
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请选择一位司机!"
      })
      return
    }
    let updateOrder = {
      orderId:this.data.orderId,
      newStatus:3,
      driverId: this.data.selectDriverId
    };
    http.updateOrderStatus([updateOrder]).then(res => {
      if (res.code == 0) {
        wx.showToast({
          duration: 2000,
          icon: 'none',
          title: '发货成功'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);
       
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})